package cn.ctyun.ipush.dao;

import cn.ctyun.ipush.model.DataSourceModel;
import cn.ctyun.ipush.utils.DateUtils;
import cn.ctyun.ipush.vo.DataSourceVo;
import com.alibaba.fastjson.JSON;
import com.dexcoder.dal.JdbcDao;
import com.dexcoder.dal.build.Criteria;
import com.dexcoder.dal.spring.page.PageControl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: user
 * Date: 16-6-18
 * Time: 下午12:56
 * To change this template use File | Settings | File Templates.
 */
@Component
public class DataSourceDao {

    @Autowired
    private JdbcDao jdbcDao;

    @Autowired
    private PageControl pageControl;

    /**
     * 保存
     *
     * @param dataSourceModel
     */
    public void save(DataSourceModel dataSourceModel) {
        jdbcDao.save(dataSourceModel);
    }

    /**
     * 更新 ,主键Id 必须存在;此数据是修改后的完整对象
     *
     * @param dataSourceModel
     */
    public int update(DataSourceModel dataSourceModel) {
        if (StringUtils.isEmpty(dataSourceModel.getDataSourceModelId()))
            throw new RuntimeException("dataSourceModel 主键id 必须设置");
        dataSourceModel.setModifyDate(DateUtils.dateToString(new Date()));
        return jdbcDao.update(dataSourceModel);
    }

    /**
     * 更新 主键Id 必须存在
     *
     * @param dataSourceModelId
     */
    public int delete(String dataSourceModelId) {
        if (StringUtils.isEmpty(dataSourceModelId))
            throw new RuntimeException("dataSourceModel 主键id 必须设置");
        Criteria criteria = Criteria.update(DataSourceModel.class).where("dataSourceModelId", new Object[]{dataSourceModelId}).set("isDeleted", "1");
        return jdbcDao.update(criteria);
    }



    /**
     * 查询单个,根据ID
     *
     * @param dataSourceModelId
     * @return
     */
    public DataSourceModel querySingleResult(String dataSourceModelId) {
        if (StringUtils.isEmpty(dataSourceModelId))
            throw new RuntimeException("dataSourceModel 主键id 必须设置");
        Criteria criteria = Criteria.select(DataSourceModel.class).where("dataSourceModelId", new Object[]{dataSourceModelId}).where("and isDeleted", new Object[]{"0"});
        return jdbcDao.querySingleResult(criteria);
    }

    /**
     * 查询单个，根据名称
     *
     * @param dataSourceName
     * @return
     */
    public DataSourceModel querySingleResultByName(String dataSourceName) {
        if (StringUtils.isEmpty(dataSourceName))
            throw new RuntimeException("dataSourceName 为空");
        Criteria criteria = Criteria.select(DataSourceModel.class).where("dataSourceName", new Object[]{dataSourceName}).where("and isDeleted", new Object[]{"0"});
        return jdbcDao.querySingleResult(criteria);
    }




    /**
     * 模糊查询多个
     *
     * @param criteria
     * @return
     */
    public List<DataSourceModel> queryList(Criteria criteria) {
        return jdbcDao.queryList(criteria);
    }

    /**
     * 精确查询多个
     *
     * @param dataSourceModel
     * @return
     */
    public List<DataSourceModel> queryList(DataSourceModel dataSourceModel) {
        return jdbcDao.queryList(dataSourceModel);
    }

    /**
     * 查询全部
     *
     * @param dataSourceModel
     * @return
     */
    public List<DataSourceModel> queryAll(DataSourceModel dataSourceModel) {
        dataSourceModel.setIsDeleted("0");
        return jdbcDao.queryList(dataSourceModel);
    }




    /**
     * 分页按条件查询
     *
     * @param currentPage
     * @param itemPages
     * @param dataSoureVo
     * @return
     */
    public List<DataSourceModel> queryListByPage(int currentPage, int itemPages, DataSourceVo dataSoureVo) {
        pageControl.performPage(currentPage, itemPages);
        Criteria criteria = Criteria.select(DataSourceModel.class).where("dataSourceName", " like ", new Object[]{"%t%"});
        jdbcDao.queryList(criteria);
        return pageControl.getPager().getList(DataSourceModel.class);
    }




    public List<DataSourceModel> queryListByPageWithUserId(int currentPage, int itemPages, DataSourceVo dataSoureVo) {
        pageControl.performPage(currentPage, itemPages);
        Criteria criteria = Criteria.select(DataSourceModel.class).where("dataSourceName", " like ", new Object[]{"%"+dataSoureVo.getDataSourceName()+"%"});
        jdbcDao.queryList(criteria);
        return pageControl.getPager().getList(DataSourceModel.class);
    }

    /**
     * sql 查询
     * @param dataSourceModel
     * @return
     */
    public List<DataSourceModel> queryBySql( DataSourceModel dataSourceModel) {
        //pageControl.performPage(1, 10);
        dataSourceModel.setDataSourceName("testdb");
        List<DataSourceModel> dataSourceModelList = jdbcDao.queryListForSql("cn.ctyun.ipush.model.DataSourceModel.getData", "params", new Object[]{dataSourceModel, null}, DataSourceModel.class);
        return dataSourceModelList;
    }
}
